<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: FormSelect.php 20096 2010-01-06 02:05:09Z bkarwin $
 */


/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';


/**
 * Helper to generate "select" list of options
 *
 * @category   Zend
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_View_Helper_FormSelectApoio extends Zend_View_Helper_FormSelect
{
    public function formSelectApoio($name, $value = null, $attribs = null, $options = null, $listsep = "<br />\n")
    {
        if (empty($attribs['action'])) {
            throw new Exception('Form Select Apoio [' . $name . '] não possui action');
        }
        $name      = $this->view->escape($name);
        $elementId = 'adicionar-' . $name;
        $xhtml  = '<span id="span-' . $name . '">' . parent::formSelect($name, $value, $attribs, $options, $listsep) . '</span>';
        $xhtml .= '<span '
               .  'class="ev-adicionar-apoio" '
               .  'id="' . $elementId . '" '
               .  'sourceId="span-' . $attribs['id'] . '" '
               .  'title="' . $attribs['title'] . '" '
               .  'action="' . $attribs['action'] . '" >+</span>';

        $js = '$().ready(function(){
                $(".ev-adicionar-apoio").click(function() {
                    var titulo   = "Adicionar " + $(this).attr("title");
                    var sourceId = $(this).attr("sourceId");
                    $("#ui-dialog-title-dialog-form-apoio").html(titulo)
                    $("#dialog-form-apoio").load($(this).attr("action"), null, function() {
                        $(".form-modal").ajaxForm(function(responseText) {
                            $("#" + sourceId).html(responseText);
                            $("#dialog-form-apoio").html("");
                            $("#dialog-form-apoio").dialog("close");
                            return false;
                        });
                    });

                    $("#dialog-form-apoio").dialog("open");
                    return false;
                });
            });';

        $this->view->headScript()->appendScript($js);
        return $xhtml;
    }
}
